<?php

class DB_Base extends DB_Pdo {

    public function __construct ()
    {
        parent::__construct ();
    }

    public function tableName ()
    {
        return $this->_table;
    }

    public function getTableName ()
    {
        $_config = Yaf_Registry::get ( 'config' )->database->mysql->toArray ();
        $prefix  = $_config[ 'prefix' ];
        return $prefix . $this->_table;
    }

    /**
     * 根据ID获取单条数据记录
     * @param int $id
     * @return array
     */
    final public function find($id = 0)
    {
        if (!$id) return array();
        $this->where('id', '=', $id);
        $info = $this->select(array('*'))
            ->build();
        $ret = $info ? $info[0] : array();
        return $ret;
    }

    /**
     * 获取单条数据记录
     *
     * @param array $fields
     * @param array $cond
     * @return array
     */
    final public function getOne ($fields = array ( '*' ) , $cond = array ())
    {
       
        if ( $cond )
        {
            foreach ( $cond as $value )
            {
                if ( isset ( $value[ 2 ] ) )
                {
                    $this->where ( $value[ 0 ] , $value[ 1 ] , $value[ 2 ] );
                }
                elseif ( is_array ( $value[ 1 ] ) )
                {
                    $this->whereIn ( $value[ 0 ] , $value[ 1 ] );
                }
                else
                {
                    $this->where ( $value[ 0 ] , '=' , $value[ 1 ] );
                }
            }
        }
        
        $info = $this->select ( $fields )
                ->build ();
        
        $ret  = $info ? $info[ 0 ] : array ();

        return $ret;
    }

    /**
     * 根据条件获取数据  适用于多条件 且 包含wherein 且可以省略 “=” 条件
     *
     * @param array $cond
     * @param array $fields
     * @return array
     * @throws YExceptionTool
     */
    final public function getRows ($fields = array ( '*' ) , $cond = array () , $order = array () , $limit = 0)
    {
        if ( $cond )
        {
            foreach ( $cond as $value )
            {
                if ( isset ( $value[ 2 ] ) )
                {
                    $this->where ( $value[ 0 ] , $value[ 1 ] , $value[ 2 ] );
                }
                elseif ( is_array ( $value[ 1 ] ) )
                {
                    $this->whereIn ( $value[ 0 ] , $value[ 1 ] );
                }
                else
                {
                    $this->where ( $value[ 0 ] , '=' , $value[ 1 ] );
                }
            }
        }
        if ( $order )
        {
            foreach ( $order as $key => $value )
            {
                $this->orderBy ( $key , $value );
            }
        }
        if ( $limit > 0 )
        {
            $this->limit($limit);
        }
        $info = $this->select ( $fields )->build ();
        return $info ? : array ();
    }

    /**
     * 获取数据列表
     *
     * @param int $start
     * @param int $num
     * @param bool $need_total
     * @param array $cond
     * @param array $order
     * @param array $fields
     * @return array
     */
    public function getList ($start = 0 , $num = 20 , $need_total = TRUE , $cond = array () , $order = array () , $fields = array ( '*' ))
    {
        $ret = array ();
        if ( $need_total )
        {
            if ( $cond )
            {
                foreach ( $cond as $value )
                {
                    if ( isset ( $value[ 2 ] ) )
                    {
                        $this->where ( $value[ 0 ] , $value[ 1 ] , $value[ 2 ] );
                    }
                    elseif ( is_array ( $value[ 1 ] ) )
                    {
                        $this->whereIn ( $value[ 0 ] , $value[ 1 ] );
                    }
                    else
                    {
                        $this->where ( $value[ 0 ] , '=' , $value[ 1 ] );
                    }
                }
            }
            $info           = $this->select ( 'COUNT(*) AS total' )->build ();
            $ret[ 'total' ] = $info && isset ( $info[ 0 ] ) && isset ( $info[ 0 ][ 'total' ] ) ? $info[ 0 ][ 'total' ] : 0;
        }

        if ( $cond )
        {
            foreach ( $cond as $value )
            {
                if ( isset ( $value[ 2 ] ) )
                {
                    $this->where ( $value[ 0 ] , $value[ 1 ] , $value[ 2 ] );
                }
                elseif ( is_array ( $value[ 1 ] ) )
                {
                    $this->whereIn ( $value[ 0 ] , $value[ 1 ] );
                }
                else
                {
                    $this->where ( $value[ 0 ] , '=' , $value[ 1 ] );
                }
            }
        }
        if ( $order )
        {
            foreach ( $order as $key => $value )
            {
                $this->orderBy ( $key , $value );
            }
        }
    
        $info          = $this->select ( $fields )
                ->limit ( $num , $start )
                ->build ();
        $ret[ 'list' ] = $info ? : array ();
        return $ret;
    }

    /**
     * 新增数据，支持单条/多条
     *
     * @param array $data
     * @param bool $need_multi
     * @return array|bool|string
     */
    public function add (array $data , $need_multi = FALSE)
    {
        $need_multi ? $this->insertMulti ( $data ) : $this->insert ( $data );
        $ret = $this->build ();
        return $ret ? $ret : false;
    }

    /**
     * 数据更新
     *
     * @param array $data
     * @param array $cond
     * @return array|bool|string
     */
    public function edit (array $data , $cond = array ())
    {
        foreach ( $cond as $value )
        {
            if ( isset ( $value[ 2 ] ) )
            {
                $this->where ( $value[ 0 ] , $value[ 1 ] , $value[ 2 ] );
            }
            elseif ( is_array ( $value[ 1 ] ) )
            {
                $this->whereIn ( $value[ 0 ] , $value[ 1 ] );
            }
            else
            {
                $this->where ( $value[ 0 ] , '=' , $value[ 1 ] );
            }
        }
        
        $ret = $this->update ( $data )
                ->build ();

        return $ret;
    }

    /**
     * 数据删除
     *
     * @param array $cond
     * @return array|bool|string
     */
    public function del (array $cond)
    {
        foreach ( $cond as $value )
        {
            if ( isset ( $value[ 2 ] ) )
            {
                $this->where ( $value[ 0 ] , $value[ 1 ] , $value[ 2 ] );
            }
            elseif ( is_array ( $value[ 1 ] ) )
            {
                $this->whereIn ( $value[ 0 ] , $value[ 1 ] );
            }
            else
            {
                $this->where ( $value[ 0 ] , '=' , $value[ 1 ] );
            }
        }

        $ret = $this->delete ()
                ->build ();

        return $ret;
    }


}
